package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// AttendanceRecord 保存打卡记录
type AttendanceRecord struct {
	ent.Schema
}

// Fields of the AttendanceRecord
func (AttendanceRecord) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Immutable().
			Comment("打卡记录ID"),
		field.Int64("user_id").
			Comment("用户ID"),
		field.Time("check_in_time").
			Optional().
			Nillable().
			Comment("上班打卡时间"),
		field.Time("check_out_time").
			Optional().
			Nillable().
			Comment("下班打卡时间"),
		field.Time("work_date").
			Default(func() time.Time {
				return time.Now().Truncate(24 * time.Hour)
			}).
			Comment("工作日期"),
		field.Enum("status").
			Values("normal", "late", "early_leave", "absent", "leave").
			Default("normal").
			Comment("打卡状态"),
		field.String("remarks").
			Optional().
			Comment("备注"),
		field.Time("created_at").
			Default(time.Now).
			Immutable().
			Comment("创建时间"),
		field.Time("updated_at").
			Default(time.Now).
			UpdateDefault(time.Now).
			Comment("更新时间"),
	}
}

// Edges of the AttendanceRecord
func (AttendanceRecord) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("user", User.Type).
			Ref("attendance_records").
			Field("user_id").
			Unique().
			Required().
			Comment("打卡用户"),
	}
}

// Indexes of the AttendanceRecord
func (AttendanceRecord) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("user_id", "work_date").
			Unique(),
		index.Fields("work_date"),
		index.Fields("user_id"),
	}
}

// Annotations of the AttendanceRecord
func (AttendanceRecord) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "attendance_records",
		},
	}
}
